package com.ruoyi.leave.service.impl;

import com.ruoyi.common.utils.DocUtils;
import com.ruoyi.common.utils.OfficeUtils;
import com.ruoyi.leave.domain.LeaveApply;
import com.ruoyi.leave.service.IHolidayService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@Service
public class HolidayServiceImpl implements IHolidayService {

    @Resource
    private ResourceLoader resourceLoader;


    @Override
    public XWPFDocument getHoliday(LeaveApply query) throws IOException {
            FileInputStream fileInputStream = DocUtils.convertInputStreamToFileInputStream(resourceLoader.getResource("classpath:templates/doc2.docx").getInputStream());
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            // 替换文本数据构建
            OfficeUtils.paragraphTextFilling(xwpfDocument,OfficeUtils.objectToMap(query));
            return xwpfDocument;
    }

    @Override
    public void getHolidayWord(XWPFDocument document, LeaveApply query, HttpServletResponse response) {
        OfficeUtils.processingWordResponses("假期请假书", OfficeUtils.writeDocumentToInputStream(document), response);
    }

    @Override
    public XWPFDocument getCourse(LeaveApply query) throws IOException {
        FileInputStream fileInputStream = DocUtils.convertInputStreamToFileInputStream(resourceLoader.getResource("classpath:templates/doc3.docx").getInputStream());
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        // 替换文本数据构建
        OfficeUtils.paragraphTextFilling(xwpfDocument,OfficeUtils.objectToMap(query));
        return xwpfDocument;
    }

    @Override
    public void getCourseWord(XWPFDocument document, LeaveApply query, HttpServletResponse response) {
        OfficeUtils.processingWordResponses("课程请假书", OfficeUtils.writeDocumentToInputStream(document), response);
    }

}
